<template>
  <div class='home-category' @mouseleave="id=''">
    <ul class="menu" >
      <!-- 遍历左边列表渲染页面 -->
      <li v-for="i in list" :key="i.id" @mouseenter="id=i.id" :class="{'showcolor':i.id===id}"  >
        <!-- 这是第一段 -->
         <RouterLink :to="`/product/${i.id}`">{{i.name}}</RouterLink>
        <template  v-if="i.children"  >
          <!-- 这是另外的两段,如果存在就显示 -->
        <RouterLink v-for="l in i.children" :key="l.id" to="/">{{l.name}}</RouterLink >

        </template>
      <span v-else>
        <!-- 如果上面不存在就显示下面的 -->
         <XtxSkeleton width="60px" height="18px" style="margin-right:5px" bg="rgba(255,255,255,0.5)" />
         <XtxSkeleton width="50px" height="18px" bg="rgba(255,255,255,0.5)" />
       </span>

      </li>
    </ul>

        <!-- 弹层 -->
    <div class="layer">
      <!-- 如果当前的id===18895就显示品牌,否则显示分类 -->
      <h4>{{id===18895?'品牌推荐':'分类推荐'}} <small>根据您的购买或浏览记录推荐</small></h4>
      <!-- 如果orr存在,显示(其他的显示不包括品牌) -->
      <ul  v-if="orr">
        <li v-for="a in orr.goods" :key="a">
          <RouterLink to="/">
            <img :src="a.picture" alt="">
            <div class="info">
              <p class="name ellipsis-2">{{a.name}}</p>
              <p class="desc ellipsis">{{a.desc}}</p>
              <p class="price"><i>¥</i>{{a.price}}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
      <!-- 如果当前id===18895就显示品牌,(18895是品牌的id) -->
      <ul  v-if="id===18895">
      <li class="brand" v-for="i in brand.brands" :key="i.id">
        <RouterLink to="/">
          <img :src="i.picture" alt="">
          <div class="info">
            <p class="place"><i class="iconfont icon-dingwei"></i>{{i.place}}</p>
            <p class="name ellipsis">{{i.name}}}</p>
            <p class="desc ellipsis-2">{{i.desc}}</p>
          </div>
        </RouterLink>
      </li>
    </ul>
    </div>
  </div>
</template>

<script>
// 引入获取品牌数据的api
import { findBrand } from '@/api/category'
// 引入vuex
import { useStore } from 'vuex'
// 引入计算属性,响应式变量,响应式对象
import { computed, ref, reactive } from 'vue'
export default {
  name: 'HomeCategory',
  setup () {
    const store = useStore()

    const list = computed(() => {
      const arr = store.state.category.list.map(v => {
        return {
          ...v,
          children: v.children && v.children.slice(0, 2)
        }
      })
      arr.push({ id: 18895, name: '品牌', children: [{ id: 1008611, name: '品牌推荐' }] })
      return arr
    })

    let id = ref(null)
    const show = (i) => {
      id = i
    }

    const orr = computed(() => {
      return list.value.find(v => v.id === id.value)
    })
    const brand = reactive(
      // 定义一级菜单数据
      { id: 18895, name: '品牌', children: [{ id: 1008611, name: '品牌推荐' }] }
    )
    findBrand(6).then(({ result }) => {
      console.log(result)
      brand.brands = result
    })
    return { list, show, orr, id, brand }
  }
}
</script>

<style scoped lang='less'>

.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0,0,0,0.8);
  position: relative;
  z-index: 99;
    &:hover {
    .layer {
      display: block;
    }
  }
  .menu {
    li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;
      &:hover,&.showcolor {
        background: @xtxColor;
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child {
          font-size: 16px;
        }
      }
    }
  }
}
.layer {
    width: 990px;
    height: 500px;
    background: rgba(255,255,255,0.8);
    position: absolute;
    left: 250px;
    top: 0;
    display: none;
    padding: 0 15px;
      li.brand {
        height: 180px;
        a {
          align-items: flex-start;
          img {
            width: 120px;
            height: 160px;
          }
          .info {
            p {
              margin-top: 8px;
            }
            .place {
              color: #999;
            }
          }
        }
      }
    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;
      small {
        font-size: 16px;
        color: #666;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background: #fff;
        &:nth-child(3n) {
          margin-right: 0;
        }
        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;
          &:hover {
            background: #e3f9f4;
          }
          img {
              width: 95px;
              height: 95px;
          }
          .info {
            padding-left: 10px;
            line-height: 24px;
       width: 190px;
            .name {
              font-size: 16px;
              color: #666;
            }
            .desc {
              color: #999;
            }
            .price {
              font-size: 22px;
              color: @priceColor;
              i {
                font-size: 16px;
              }
            }
          }
        }
      }
    }
  }

</style>
